package web.controller;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.itextpdf.text.DocumentException;

import dao.IDaoAsof;

import service.authentification.Contributor;
import service.pdfgeneration.IPDFGenerator;
import service.sheetmanager.ISheetManager;
import web.form.SheetView;

/**
 * <b>SheetController  </b>
 * <p>SheetController controle les requêtes pour consulter une fiches sous format html ou pdf.</p>
 * 
 */
@Controller
public class SheetController {
	
	/**
     * Repr&eacute;sente le service de gestion de fiches
     */
	@Resource
	ISheetManager sheetManager;

	/**
     * Repr&eacute;sente le service de génération de pdf
     */
	@Resource
	IPDFGenerator pdfGenerator;
	
	/**
     * Repr&eacute;sente le service d'accés aux données
     */
	@Resource
	IDaoAsof dao;
	
	/**
     * Controlleur qui renvoi la page d'une fiche
     */
	@RequestMapping(value = "/sheet.htm", method = RequestMethod.GET)
	public String handleRequest(HttpSession session, ModelMap model,
			@RequestParam(value="code", required=false) String code,
			@RequestParam(value="type", required=false) String type)
			  throws ServletException, IOException{
		
			SheetView sheetView = sheetManager.findSheet(code,type);
			if(sheetView == null) {
				return "error/notFound";
			}
			Contributor user=(Contributor)session.getAttribute("user");
			if(user!=null){
				if(sheetManager.personIsAllowedToModifySheet(user.getCode(), code))
					model.addAttribute("personIsAlowedToModify", true);
				if(dao.personOwnsSheet(user.getCode(), code))
					model.addAttribute("personIsOwner", true);
				
			}	
			model.addAttribute("sheetView", sheetView);
			
			return "sheet";
	}
	
	/**
     * Controlleur qui renvoi génère un pdf via le service de génération pdf et le renvoi vers le client
     */
	@RequestMapping(value = "/pdf.htm", method = RequestMethod.GET)
	public String generatePDF(HttpSession session, ModelMap model, HttpServletResponse response,
							  @RequestParam(value="code", required=false) String code,
							  @RequestParam(value="type", required=false) String type)
							  throws ServletException, IOException{
		
		SheetView sheetView = sheetManager.findSheet(code, type);
        if(sheetView == null) {
            return "error/notFound";
        }
		
		try {
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "inline; filename=\"" + sheetView.getCode() + ".pdf\"");
			pdfGenerator.generatePdf(sheetView, response.getOutputStream());
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
